<template>
    <div id="payContainer">
        <div id="headTitle">缴纳班费</div>
        <div id="showPayproject">
            <el-input id="searchTextInput" v-model="searchTitle" placeholder="请输入搜索的内容"/>
            <el-button type="primary" @click="loadPaymentProjectData">搜索</el-button>
        </div>
        <div id="table">
            <el-table max-height="700" :data="paymentProjects" border>
                <el-table-column type="expand">
                    <template #default="props">
                        <div class="tableRowContainer">
                            <p class="tableRow">项目名：{{ props.row.payProjTitle }}</p>
                            <p class="tableRow">发布者：{{ props.row.releaseUser }}</p>
                            <p class="tableRow">发布时间：{{ props.row.releaseTime }}</p>
                            <p class="tableRow">截止时间：{{ props.row.deadLine }}</p>
                            <p class="tableRow">缴费金额：{{ props.row.payProjAmount }}</p>
                            <p class="tableRow">项目简介：<br>{{ props.row.payProjBrief }}</p>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="payProjID" label="项目ID" show-overflow-tooltip align="center" width="100"/>
                <el-table-column prop="payProjTitle" label="项目名" show-overflow-tooltip align="center"/>
                <el-table-column prop="payProjAmount" label="缴费金额" show-overflow-tooltip align="center" width="200"/>
                <el-table-column prop="releaseTime" label="发布时间" show-overflow-tooltip align="center" width="200"/>
                <el-table-column prop="deadLine" label="截止时间" show-overflow-tooltip align="center" width="200"/>
                <el-table-column label="操作" width="150" align="center">
                    <template #default="scope">
                        <el-button type="primary" v-if="scope.row.isDelete == '否'" plain @click="pay(scope.row.payProjID)">立即缴费</el-button>
                        <el-button type="info" v-if="scope.row.isDelete == '是'" plain disabled="true">已删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div id="pagination">
            <el-pagination
                    v-model:current-page="pageNumber"
                    v-model:page-size="pageSize"
                    :page-sizes="[5, 10, 20, 50]"
                    :small=false
                    :disabled=false
                    :background=false
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="paymentProjectTotal"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script>
//css
import "../assets/Pay.css";
//请求
import request from '@/utils/requset.js';
//顶部提示信息
import prompt from '@/utils/prompt.js'
//路由跳转
import router from "@/router";

export default {
    name: 'classInfo',
    data() {
        return {
            //缴费项目数据
            paymentProjects: [],
            pageNumber: 1,
            pageSize: 5,
            paymentProjectTotal: 0,
            searchTitle: "",
        }
    },
    created() {
        if (this.isIntoClass()) {
            this.loadPaymentProjectData();
        }
    },
    methods: {
        //是否加入班级
        isIntoClass() {
            let classID = localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")).classID : 0;
            if (classID < 1) {
                prompt("还未加入班级！", "error");
                router.push({path: "home"});
                return false;
            }
            return true;
        },
        //加载缴费项目信息
        loadPaymentProjectData() {
            let token = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            if (token === "") {
                prompt("还未登录！", "error");
                router.push({path: "/login"});
                return;
            }
            request.get("/paymentProject/infoForUser", {
                params: {
                    token: token,
                    pageNumber: this.pageNumber,
                    pageSize: this.pageSize,
                    searchTitle: this.searchTitle,
                }
            }).then(result => {
                //请求失败
                if (result.code !== "200") {
                    prompt(result.message, result.type);
                }
                //请求成功
                this.paymentProjects = result.data.paymentProjects;
                this.paymentProjectTotal = result.data.paymentProjectTotal;
            })
        },
        //缴纳班费
        pay(paymentProjectID) {
            let token = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            request.get(
                "/paymentProject/pay", {
                    params: {
                        token,
                        paymentProjectID
                    }
                }
            ).then(result => {
                if (result.code != "200") {
                    prompt("缴费失败！", result.type);
                    return;
                }
                //请求成功
                prompt("缴费成功！", result.type);
                //加载新数据
                this.loadPaymentProjectData();
            });
        },
        //分页-单页数量
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loadPaymentProjectData();
        },
        //分页-页码
        handleCurrentChange(pageNumber) {
            this.pageNumber = pageNumber;
            this.loadPaymentProjectData();
        },
    }
}
</script>